package com.guonl.controller;

import com.sftc.tech.kmsclient.KmsClient;
import com.sftc.tech.kmsclient.dto.DecryptRequest;
import com.sftc.tech.kmsclient.dto.DecryptResponse;
import com.sftc.tech.kmsclient.dto.EncryptRequest;
import com.sftc.tech.kmsclient.dto.EncryptResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据库字段加解密测试
 */
@Slf4j
@RestController
@RequestMapping("/kms")
public class KmsController {

    private KmsClient kmsClient;

    public KmsController(KmsClient kmsClient) {
        this.kmsClient = kmsClient;
    }

    /**
     * kms 加密
     */
    @GetMapping("/encrypt")
    public ResponseEntity kmsEncrypt(String content) throws Exception {

        // 加密示例
        EncryptRequest encryptReq = new EncryptRequest();
        encryptReq.setSystemCode("rider.loc.svc");
        EncryptRequest.EncryptData encryptData = new EncryptRequest.EncryptData();
        encryptData.setCmkIndex("001");
        encryptData.setPlainText(content);
        List<EncryptRequest.EncryptData> encryptDataList = new ArrayList<>();
        encryptDataList.add(encryptData);
        encryptReq.setEncryptDataList(encryptDataList);

        EncryptResponse encryptResp = kmsClient.encrypt(encryptReq);
        System.out.println("kms 加密结果:" + encryptResp);

        return ResponseEntity.ok(encryptResp);
    }

    /**
     * kms 解密
     */
    @GetMapping("/decrypt")
    public ResponseEntity kmsDecrypt(String content) throws Exception {

        //解密示例
        DecryptRequest decryptReq = new DecryptRequest();
        decryptReq.setSystemCode("rider.loc.svc");

        List<String> deDataList = new ArrayList<>();
        deDataList.add(content);

        decryptReq.setDataList(deDataList);
        DecryptResponse decryptResp = kmsClient.decrypt(decryptReq);
        System.out.println("kms 解密结果:" + decryptResp);
        return ResponseEntity.ok(decryptResp);

    }

    /**
     * kms hashCode
     */
    @GetMapping("/hashCode")
    public ResponseEntity kmsHashCode(String content) throws Exception {

        //hashCode示例
        String hashCode = kmsClient.hashCode(content, "phone");

        System.out.println("kms hashCode结果:" + hashCode);
        return ResponseEntity.ok(hashCode);
    }

    @GetMapping("/hashCode/demo")
    public ResponseEntity kmsHashCodeDemo() throws Exception {

        String mobile = "18587791992";
        String idCardNo = "450106199208171035";
        String bankCardNo = "6217994910086003458";
        String address = "江苏省常州市金坛区东城街道顺丰速运江苏省常州市金坛区东城街道顺丰速运江苏省常州市金坛区东城街道顺丰速运江苏省常州市金坛区东城街道顺丰速运江苏省常州市金坛区东城街道顺丰速运江苏省常州市金坛区东城街道顺";
        String passwd = "4a52a35306b8487a3e1034e4bac0846f";

        //hashCode示例
        String mobileHashCode = kmsClient.hashCode(mobile, "phone");
        String idCardNoHashCode = kmsClient.hashCode(idCardNo, "idcard");
        String bankCardNoHashCode = kmsClient.hashCode(bankCardNo, "idcard");
        String addressHashCode = kmsClient.hashCode(address, "idcard");
        String passwdHashCode = kmsClient.hashCode(passwd, "idcard");

        System.out.println("手机号，加密前长度：" + mobile.length() + "，hashCode后长度：" + mobileHashCode.length());
        System.out.println("身份证，加密前长度：" + idCardNo.length() + "，hashCode后长度：" + idCardNoHashCode.length());
        System.out.println("银行卡，加密前长度：" + bankCardNo.length() + "，hashCode后长度：" + bankCardNoHashCode.length());
        System.out.println("地址，加密前长度：" + address.length() + "，hashCode后长度：" + addressHashCode.length());
        System.out.println("密码，加密前长度：" + passwd.length() + "，hashCode后长度：" + passwdHashCode.length());

        return ResponseEntity.ok("OK");
    }

}
